<template>
  <div class="app-container">
    <el-form size="mini" :inline="true" :model="queryParams" @keyup.enter.native="loadData()">
      <el-form-item label="仓库">
        <el-select style="width:160px;" v-model="queryParams.wareId" placeholder="请选择仓库" clearable>
          <el-option :label="item.name" :value="item.id" v-for="item in wareList" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="SKU">
        <sku-search-select :sku-id.sync="skuId"></sku-search-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="loadData">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="loading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
    >
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="wareName" header-align="center" align="center" label="仓库"></el-table-column>
      <el-table-column prop="skuName" header-align="center" align="center" label="商品名称">
        <template slot-scope="scope">
          <el-tooltip v-if="scope.row.skuName" class="item" effect="dark" :content="scope.row.skuName" placement="top-start">
            <span> {{ scope.row.skuName.length <= 30 ? scope.row.skuName : (scope.row.skuName.substr(0,30) + '...')}} </span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="stock" header-align="center" align="center" label="库存数"></el-table-column>
      <el-table-column prop="stockLocked" header-align="center" align="center" label="锁定库存"></el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-delete" size="small" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="loadData"
    />

    <ware-sku-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="loadData"></ware-sku-form>
  </div>
</template>

<script>
import wareSkuForm from "./wareSkuForm.vue";
import {deleteWareSku, wareSkuList} from '@/api/ware/wareSku'
import {listAllWare} from "@/api/ware/ware";
import SkuSearchSelect from "@/views/common/skuSearchSelect.vue";

export default {
  data() {
    return {
      wareList: [],
      queryParams: {
        wareId: '',
        skuId: '',
        pageNo: 1,
        pageSize: 10
      },
      skuId: '',
      dataList: [],
      total: 0,
      loading: false,
      selections: [],
      addOrUpdateVisible: false
    };
  },
  components: {
    SkuSearchSelect,
    wareSkuForm
  },
  watch: {
    skuId(val) {
      this.queryParams.skuId = val
    }
  },
  created() {
    if (this.$route.query.skuId) {
      this.dataForm.skuId = this.$route.query.skuId;
    }
    this.getWares();
    this.loadData();
  },
  methods: {
    getWares() {
      listAllWare().then(res => {
        if (res.success) {
          this.wareList = res.data
        }
      });
    },
    // 获取数据列表
    loadData() {
      this.loading = true;
      wareSkuList(this.queryParams).then(res => {
        if (res.success) {
          this.dataList = res.data.records;
          this.total = res.data.total;
        }
        this.loading = false;
      });
    },
    // 多选
    selectionChangeHandle(val) {
      this.selections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },

    resetQuery() {
      this.queryParams= {
        pageNo: 1,
        pageSize: 10,
        wareId: '',
        skuId: '',
      }
      this.skuId = ''
      this.loadData()
    },

    // 删除
    handleDelete(id) {
      let ids = id ? [id] : this.selections.map(item => {return item.id;});
      this.$confirm(
        '确定删除选中数据吗?',
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        deleteWareSku(ids).then(res => {
          if (res.success) {
            this.$message.success('删除成功')
            this.loadData()
          }
        });
      });
    }
  }
};
</script>
